package net.cardgame.orcalecard.asynctask;

import net.cardgame.orcalecard.utils.ConstantValue;
import net.cardgame.orcalecard.utils.NetworkUtils;
import android.content.Context;
import android.os.AsyncTask;

public class GetListDeckTask extends AsyncTask<String, Integer, String> {

	private DownloadListener downloadListener;
	private NetworkUtils networkUtils;
	private Context context;

	public GetListDeckTask(Context context) {
		this.context = context;
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		networkUtils = new NetworkUtils();
	}

	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub

		String deckXml = "";
		if (NetworkUtils.isNetworkConnected(context)) {
			deckXml = networkUtils.getStringFromUrl(ConstantValue.BASE_URL
					+ ConstantValue.LIST_DECK_SUB_URL);
		}
		return deckXml;
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);

		// if download success call this
		if (downloadListener != null && result != "") {
			downloadListener.onDownloadSuccessListener(result);
		}
		// if download fail call this
		if ((downloadListener != null && result == "")
				|| downloadListener != null && result == null) {
			downloadListener.onDownloadFailListener(result);
		}
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
		if (downloadListener != null) {
			downloadListener.onProgressUpdateListener(values[0]);
		}
	}

	/**
	 * @param downloadListener
	 *            the downloadListener to set
	 */
	public void setDownloadListener(DownloadListener downloadListener) {
		this.downloadListener = downloadListener;
	}

}
